using SM;
using SS;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace TVSBH.func
{
	public class frmSelectProductCode : Form
	{
		private IContainer components = null;
		private Grid dgvProducts;
		private DataGridViewTextBoxColumn possID;
		private DataGridViewTextBoxColumn possName;
		public TextBox txtSearch;
		private Label label1;
		private Label label2;
		private DataGridViewTextBoxColumn Tensanpham;
		private DataGridViewTextBoxColumn Donvitinh;
		private DataGridViewTextBoxColumn dgvPacket;
		private DataGridViewTextBoxColumn Masanpham;
		private DataGridViewTextBoxColumn Gianhap;
		private DataGridViewTextBoxColumn Giabanbuon;
		private DataGridViewTextBoxColumn Giabanle;
		private DataGridViewTextBoxColumn P_possName;
		private DataGridViewTextBoxColumn Balancer;
		private DataGridViewTextBoxColumn ProductExpiryDate;
		private Func objFunc = new Func();
		public string productID = "";
		public string productName = "";
		public string priceBase = "";
		public string priceTrade = "";
		public string priceRetail = "";
		public string unitName = "";
		public string nationalName = "";
		public string productPossName = "";
		public string packet;
		private bool Loading = false;
		public bool isInput = false;
		public bool sellWithBill;
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle3 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle4 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle5 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle6 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle7 = new DataGridViewCellStyle();
			ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(frmSelectProductCode));
			this.txtSearch = new TextBox();
			this.label1 = new Label();
			this.label2 = new Label();
			this.dgvProducts = new Grid();
			this.Tensanpham = new DataGridViewTextBoxColumn();
			this.Donvitinh = new DataGridViewTextBoxColumn();
			this.dgvPacket = new DataGridViewTextBoxColumn();
			this.Masanpham = new DataGridViewTextBoxColumn();
			this.Gianhap = new DataGridViewTextBoxColumn();
			this.Giabanbuon = new DataGridViewTextBoxColumn();
			this.Giabanle = new DataGridViewTextBoxColumn();
			this.P_possName = new DataGridViewTextBoxColumn();
			this.Balancer = new DataGridViewTextBoxColumn();
			this.ProductExpiryDate = new DataGridViewTextBoxColumn();
			((ISupportInitialize)this.dgvProducts).BeginInit();
			base.SuspendLayout();
			this.txtSearch.AcceptsReturn = true;
			this.txtSearch.Font = new Font("Arial", 10f);
			this.txtSearch.Location = new Point(104, 11);
			this.txtSearch.Name = "txtSearch";
			this.txtSearch.Size = new Size(228, 23);
			this.txtSearch.TabIndex = 1;
			this.txtSearch.TextChanged += new EventHandler(this.txtSearch_TextChanged);
			this.txtSearch.KeyDown += new KeyEventHandler(this.txtSearch_KeyDown);
			this.label1.AutoSize = true;
			this.label1.Location = new Point(7, 15);
			this.label1.Name = "label1";
			this.label1.Size = new Size(93, 15);
			this.label1.TabIndex = 25;
			this.label1.Text = "Tên sản phẩm :";
			this.label2.AutoSize = true;
			this.label2.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.label2.Location = new Point(356, 15);
			this.label2.Name = "label2";
			this.label2.Size = new Size(104, 15);
			this.label2.TabIndex = 26;
			this.label2.Text = "Bấn ESC để thoát";
			this.dgvProducts.AllowUserToAddRows = false;
			this.dgvProducts.AllowUserToDeleteRows = false;
			this.dgvProducts.AllowUserToResizeColumns = false;
			this.dgvProducts.AllowUserToResizeRows = false;
			dataGridViewCellStyle.BackColor = Color.FromArgb(192, 255, 255);
			dataGridViewCellStyle.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dgvProducts.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle;
			this.dgvProducts.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.dgvProducts.BackgroundColor = Color.White;
			this.dgvProducts.BorderStyle = BorderStyle.Fixed3D;
			this.dgvProducts.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
			dataGridViewCellStyle2.BackColor = SystemColors.Control;
			dataGridViewCellStyle2.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			dataGridViewCellStyle2.ForeColor = SystemColors.WindowText;
			dataGridViewCellStyle2.SelectionBackColor = Color.Aqua;
			dataGridViewCellStyle2.SelectionForeColor = Color.Black;
			this.dgvProducts.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
			this.dgvProducts.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			this.dgvProducts.Columns.AddRange(new DataGridViewColumn[]
			{
				this.Tensanpham,
				this.Donvitinh,
				this.dgvPacket,
				this.Masanpham,
				this.Gianhap,
				this.Giabanbuon,
				this.Giabanle,
				this.P_possName,
				this.Balancer,
				this.ProductExpiryDate
			});
			this.dgvProducts.Cursor = Cursors.Hand;
			this.dgvProducts.EnableHeadersVisualStyles = false;
			this.dgvProducts.Location = new Point(7, 40);
			this.dgvProducts.MultiSelect = false;
			this.dgvProducts.Name = "dgvProducts";
			this.dgvProducts.ReadOnly = true;
			dataGridViewCellStyle3.BackColor = SystemColors.Control;
			dataGridViewCellStyle3.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			dataGridViewCellStyle3.ForeColor = Color.Black;
			dataGridViewCellStyle3.SelectionBackColor = Color.Aqua;
			dataGridViewCellStyle3.SelectionForeColor = Color.Black;
			this.dgvProducts.RowHeadersDefaultCellStyle = dataGridViewCellStyle3;
			this.dgvProducts.RowHeadersVisible = false;
			this.dgvProducts.RowHeadersWidth = 26;
			this.dgvProducts.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
			dataGridViewCellStyle4.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dgvProducts.RowsDefaultCellStyle = dataGridViewCellStyle4;
			this.dgvProducts.RowTemplate.DefaultCellStyle.BackColor = Color.White;
			this.dgvProducts.RowTemplate.DefaultCellStyle.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dgvProducts.RowTemplate.DefaultCellStyle.ForeColor = Color.Black;
			this.dgvProducts.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.Aqua;
			this.dgvProducts.RowTemplate.DefaultCellStyle.SelectionForeColor = Color.Black;
			this.dgvProducts.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			this.dgvProducts.Size = new Size(485, 203);
			this.dgvProducts.TabIndex = 2;
			this.dgvProducts.CellDoubleClick += new DataGridViewCellEventHandler(this.dgvProducts_CellDoubleClick);
			this.dgvProducts.CellEnter += new DataGridViewCellEventHandler(this.dgvProducts_CellEnter);
			this.dgvProducts.KeyPress += new KeyPressEventHandler(this.dgvProducts_KeyPress);
			this.Tensanpham.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			this.Tensanpham.DataPropertyName = "P_Name";
			dataGridViewCellStyle5.Alignment = DataGridViewContentAlignment.MiddleLeft;
			dataGridViewCellStyle5.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			dataGridViewCellStyle5.NullValue = null;
			this.Tensanpham.DefaultCellStyle = dataGridViewCellStyle5;
			this.Tensanpham.HeaderText = "Tên sản phẩm";
			this.Tensanpham.MinimumWidth = 120;
			this.Tensanpham.Name = "Tensanpham";
			this.Tensanpham.ReadOnly = true;
			this.Donvitinh.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.Donvitinh.DataPropertyName = "UnitName";
			this.Donvitinh.HeaderText = "Đơn vị tính";
			this.Donvitinh.MinimumWidth = 80;
			this.Donvitinh.Name = "Donvitinh";
			this.Donvitinh.ReadOnly = true;
			this.Donvitinh.Width = 80;
			this.dgvPacket.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.dgvPacket.DataPropertyName = "Packet";
			this.dgvPacket.HeaderText = "Số lô";
			this.dgvPacket.MinimumWidth = 90;
			this.dgvPacket.Name = "dgvPacket";
			this.dgvPacket.ReadOnly = true;
			this.dgvPacket.Width = 90;
			this.Masanpham.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.Masanpham.DataPropertyName = "PID";
			this.Masanpham.HeaderText = "PID";
			this.Masanpham.Name = "Masanpham";
			this.Masanpham.ReadOnly = true;
			this.Masanpham.Resizable = DataGridViewTriState.False;
			this.Masanpham.Visible = false;
			this.Masanpham.Width = 5;
			this.Gianhap.DataPropertyName = "P_PriceBase";
			this.Gianhap.HeaderText = "Giá nhập";
			this.Gianhap.Name = "Gianhap";
			this.Gianhap.ReadOnly = true;
			this.Gianhap.Visible = false;
			this.Giabanbuon.DataPropertyName = "P_PriceTrade";
			this.Giabanbuon.HeaderText = "Giá bán buôn";
			this.Giabanbuon.Name = "Giabanbuon";
			this.Giabanbuon.ReadOnly = true;
			this.Giabanbuon.Visible = false;
			this.Giabanle.DataPropertyName = "P_PriceRetail";
			this.Giabanle.HeaderText = "Giá bán lẻ";
			this.Giabanle.Name = "Giabanle";
			this.Giabanle.ReadOnly = true;
			this.Giabanle.Visible = false;
			this.P_possName.DataPropertyName = "possName";
			this.P_possName.HeaderText = "possName";
			this.P_possName.Name = "P_possName";
			this.P_possName.ReadOnly = true;
			this.P_possName.Visible = false;
			this.Balancer.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.Balancer.DataPropertyName = "Balancer";
			dataGridViewCellStyle6.Alignment = DataGridViewContentAlignment.MiddleRight;
			dataGridViewCellStyle6.Format = "N0";
			dataGridViewCellStyle6.NullValue = "0";
			this.Balancer.DefaultCellStyle = dataGridViewCellStyle6;
			this.Balancer.HeaderText = " Tồn kho";
			this.Balancer.MinimumWidth = 60;
			this.Balancer.Name = "Balancer";
			this.Balancer.ReadOnly = true;
			this.Balancer.Width = 60;
			this.ProductExpiryDate.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.ProductExpiryDate.DataPropertyName = "ProductExpiryDate";
			dataGridViewCellStyle7.Alignment = DataGridViewContentAlignment.MiddleCenter;
			dataGridViewCellStyle7.Format = "D";
			dataGridViewCellStyle7.NullValue = null;
			this.ProductExpiryDate.DefaultCellStyle = dataGridViewCellStyle7;
			this.ProductExpiryDate.HeaderText = "Hạn sử dụng";
			this.ProductExpiryDate.MinimumWidth = 85;
			this.ProductExpiryDate.Name = "ProductExpiryDate";
			this.ProductExpiryDate.ReadOnly = true;
			this.ProductExpiryDate.Width = 85;
			base.AutoScaleDimensions = new SizeF(7f, 15f);
			//base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(499, 250);
			base.Controls.Add(this.label2);
			base.Controls.Add(this.label1);
			base.Controls.Add(this.txtSearch);
			base.Controls.Add(this.dgvProducts);
			this.Cursor = Cursors.Hand;
			this.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			//base.Icon = (System.Drawing.Icon)componentResourceManager.GetObject("$this.Icon");
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			base.KeyPreview = true;
			base.Name = "frmSelectProductCode";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.Manual;
			this.Text = "..: Lựa chọn sản phẩm";
			base.Load += new EventHandler(this.frmSelectProductCode_Load);
			base.KeyDown += new KeyEventHandler(this.frmSelectProduct_KeyDown);
			((ISupportInitialize)this.dgvProducts).EndInit();
			base.ResumeLayout(false);
			base.PerformLayout();
		}
		public frmSelectProductCode()
		{
			this.InitializeComponent();
			this.txtSearch.Focus();
		}
		private void frmSelectProductCode_Load(object sender, EventArgs e)
		{
			this.getProduct();
			this.Loading = true;
			this.txtSearch.Focus();
		}
		private void btnSelect_Click(object sender, EventArgs e)
		{
			base.Close();
		}
		private void getProduct()
		{
			this.dgvProducts.AutoGenerateColumns = false;
			SqlConnection sqlConnection = new SqlConnection(this.objFunc.strConnectionString);
			string cmdText = "SELECT TOP 500 Pid, UPPER(productName) AS P_Name, productPrice AS P_PriceBase, productPrice1 AS P_PriceTrade, productPrice2 AS P_PriceRetail, productUnit AS UnitName, PossName, Packet, ProductExpiryDate, [dbo].[uf_getBalancerProductByPacket](Pid,Packet) AS Balancer FROM [TabDetailBill] WHERE TabDetailBill.productName LIKE N'%" + this.txtSearch.Text.Trim() + "%' AND PID IN (SELECT PID FROM TabTotalBalancer WHERE productNumberOut < productNumberIn) AND BillID IN (SELECT ID FROM TabBill WHERE TypeInput IN ('IN1','IN2','IN3')) AND [dbo].[uf_getBalancerProductByPacket](Pid,Packet) > 0 ORDER BY [P_Name]";
			SqlCommand selectCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand);
			DataSet dataSet = new DataSet();
			sqlConnection.Open();
			sqlDataAdapter.Fill(dataSet);
			this.dgvProducts.DataSource = dataSet.Tables[0];
			sqlConnection.Close();
			sqlConnection.Dispose();
			sqlDataAdapter.Dispose();
			dataSet.Dispose();
		}
		private void frmSelectProduct_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
			else
			{
				if (e.KeyCode == Keys.F3)
				{
					this.txtSearch.Focus();
					this.txtSearch.SelectionStart = this.txtSearch.Text.Trim().Length;
				}
			}
		}
		private void btnClose_Click(object sender, EventArgs e)
		{
			this.productName = "";
			this.unitName = "";
			this.priceBase = "";
			this.priceTrade = "";
			this.priceRetail = "";
			this.nationalName = "";
			this.productID = "";
			base.Close();
		}
		private void dgvProducts_CellEnter(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				this.packet = this.dgvProducts.Rows[e.RowIndex].Cells["dgvPacket"].Value.ToString();
				this.productName = this.dgvProducts.Rows[e.RowIndex].Cells["Tensanpham"].Value.ToString();
				this.unitName = this.dgvProducts.Rows[e.RowIndex].Cells["Donvitinh"].Value.ToString();
				this.priceBase = this.dgvProducts.Rows[e.RowIndex].Cells["Gianhap"].Value.ToString();
				this.priceTrade = this.dgvProducts.Rows[e.RowIndex].Cells["Giabanbuon"].Value.ToString();
				this.priceRetail = this.dgvProducts.Rows[e.RowIndex].Cells["Giabanle"].Value.ToString();
				this.productID = this.dgvProducts.Rows[e.RowIndex].Cells["Masanpham"].Value.ToString();
				this.productPossName = this.dgvProducts.Rows[e.RowIndex].Cells["P_possName"].Value.ToString();
			}
			catch
			{
			}
		}
		private void dgvProducts_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			base.Close();
		}
		public void dgvProducts_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				base.Close();
			}
		}
		private void txtSearch_TextChanged(object sender, EventArgs e)
		{
			if (this.Loading)
			{
				this.getProduct();
			}
		}
		private void txtSearch_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Down)
			{
				this.dgvProducts.Focus();
			}
		}
	}
}
